<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<form id="form">
			<input type="text" name="name" placeholder="书本名称" /></br>
			<input type="text" name="price" placeholder="单价"/></br>
			<input type="date" name="tdate" placeholder="出版日期" /></br>
			<button type="button" onclick="save()">保存</button>
		</form>
		<script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
	
			let editItem = JSON.parse(sessionStorage.getItem("item"));
			console.log(editItem);
			if(editItem) {
				$("input[name='name']").val(editItem.name);
				$("input[name='price']").val(editItem.price);
				$("input[name='tdate']").val(editItem.tdate);
			}
			function save() {
				let name = $("input[name='name']").val();
				let price = $("input[name='price']").val();
				let tdate = $("input[name='tdate']").val();
				let obj = {
					id: editItem ? editItem.id : 1,
					name: name,
					price: price,
					tdate: tdate,
					count: editItem ? editItem.count : 1,
					account: sessionStorage.getItem("account")
				}
		
				let data = localStorage.getItem("data");
				if(data) {
					data = JSON.parse(data);
					for(let index in data) {
						let item = data[index];
						if(sessionStorage.getItem("account") !== item.account) {
							continue;
						}
						if(editItem) {
							
							if(editItem.name !== name && item.name === name) {
								alert("书名不能重复");
								return;
							}
						}else{
						
							if(item.name === name) {
								alert("书名不能重复");
								return;
							}
						}
					}
					if(editItem) {
						let index = sessionStorage.getItem("index");
						data.splice(index, 1, obj);
					}else {
						data.push(obj);
					}
					localStorage.setItem("data", JSON.stringify(data));
					location.href = 'index01.html';
				}
			}
		</script>
	</body>
</html>
